﻿// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.

using SixLabors.ImageSharp.ColorSpaces;
using SixLabors.ImageSharp.ColorSpaces.Conversion;

namespace SixLabors.ImageSharp.Tests.Colorspaces.Conversion;

/// <summary>
/// Tests <see cref="CieLab"/>-<see cref="CieLuv"/> conversions.
/// </summary>
/// <remarks>
/// Test data generated using:
/// <see href="http://www.brucelindbloom.com/index.html?ColorCalculator.html"/>
/// </remarks>
public class CieLabAndCieLuvConversionTests
{
    private static readonly ApproximateColorSpaceComparer ColorSpaceComparer = new ApproximateColorSpaceComparer(.0002F);
    private static readonly ColorSpaceConverter Converter = new ColorSpaceConverter();

    /// <summary>
    /// Tests conversion from <see cref="CieLuv"/> to <see cref="CieLab"/>.
    /// </summary>
    [Theory]
    [InlineData(0, 0, 0, 0, 0, 0)]
    [InlineData(10, 36.0555, 303.6901, 10.0151367, -23.9644356, 17.0226)]
    public void Convert_CieLuv_to_CieLab(float l, float u, float v, float l2, float a, float b)
    {
        // Arrange
        var input = new CieLuv(l, u, v);
        var expected = new CieLab(l2, a, b);

        Span<CieLuv> inputSpan = new CieLuv[5];
        inputSpan.Fill(input);

        Span<CieLab> actualSpan = new CieLab[5];

        // Act
        var actual = Converter.ToCieLab(input);
        Converter.Convert(inputSpan, actualSpan);

        // Assert
        Assert.Equal(expected, actual, ColorSpaceComparer);

        for (int i = 0; i < actualSpan.Length; i++)
        {
            Assert.Equal(expected, actualSpan[i], ColorSpaceComparer);
        }
    }

    /// <summary>
    /// Tests conversion from <see cref="CieLab"/> to <see cref="CieLuv"/>.
    /// </summary>
    [Theory]
    [InlineData(0, 0, 0, 0, 0, 0)]
    [InlineData(10.0151367, -23.9644356, 17.0226, 10.0000038, -12.830183, 15.1829338)]
    public void Convert_CieLab_to_CieLuv(float l, float a, float b, float l2, float u, float v)
    {
        // Arrange
        var input = new CieLab(l, a, b);
        var expected = new CieLuv(l2, u, v);

        Span<CieLab> inputSpan = new CieLab[5];
        inputSpan.Fill(input);

        Span<CieLuv> actualSpan = new CieLuv[5];

        // Act
        var actual = Converter.ToCieLuv(input);
        Converter.Convert(inputSpan, actualSpan);

        // Assert
        Assert.Equal(expected, actual, ColorSpaceComparer);

        for (int i = 0; i < actualSpan.Length; i++)
        {
            Assert.Equal(expected, actualSpan[i], ColorSpaceComparer);
        }
    }
}
